#include"cmd_mysql.h"
#include"dirStack.h"
/* int init_Usertask(dirStack_t *taskStack,char*UserName,MYSQL *connect) */
/* { */
/*     mysql_info_t mysql_info; */
/*     bzero(&mysql_info, sizeof(mysql_info_t)); */
/*     sprintf(mysql_info.sql_commd,"select id from file_info where user = '%s' and pre_id = '-1';",UserName); */
/*     read_mysql(&mysql_info,connect); */
/*     enStack(taskStack,mysql_info.sql_result); //存放用户根目录 */
/*     memcpy(taskStack->pFront->data, mysql_info.sql_result, sizeof(mysql_info.sql_result)); */
/*     return 0; */
/* } */

int read_mysql(mysql_info_t *mysql_info,MYSQL *connect)
{
    printf("c read 16:%s\n",mysql_info->sql_commd);
    int qret =  mysql_query(connect,mysql_info->sql_commd);
    //读
    MYSQL_RES *res = mysql_store_result(connect);
    MYSQL_ROW row;
    mysql_num_rows(res);
    while((row = mysql_fetch_row(res))!=NULL)
    {
        for(unsigned int i=0;i<mysql_num_fields(res);i++)
        {
            strcat(mysql_info->sql_result,row[i]);
            strcat(mysql_info->sql_result," ");
        }
    }
    // 去除末尾的空格
    // 去除末尾多余的斜杠
    if (strlen(mysql_info->sql_result) > 0 && mysql_info->sql_result[strlen(mysql_info->sql_result) - 1] == ' ') {
        mysql_info->sql_result[strlen(mysql_info->sql_result) - 1] = '\0';
    }
    if(strlen(mysql_info->sql_result)==0)
    {
        printf("readmysql:36:\n");
        strcpy(mysql_info->sql_result,".. .");
        return -1;
    }
    return 0;
}
//cd
int cd_mysql(char*UserName,dirStack_t *taskStack,char*args,MYSQL *connect,char*buf)
{
    mysql_info_t mysql_info;
    char cutPath[4096] = {0};
    //strtok
    strcpy(cutPath,args);
    printf("top = %s, cutPath = %s\n", taskStack->pFront->data, cutPath);
    char *sockPath = strtok(cutPath,"/");
    printf("sockPath = %s\n", sockPath);

    while(sockPath)
    {
        if(strcmp(sockPath,".")!=0 && strcmp(sockPath,"..")!=0)
        {
            char id[10]={0};
            bzero(id,sizeof(id));
            strcpy(id,taskStack->pFront->data);
            bzero(&mysql_info, sizeof(mysql_info_t));
            //snprintf()
            //查询  filename user pre_id type tomb
            sprintf(mysql_info.sql_commd,"select id from file_info where filename = \
                                        '%s' and  user = '%s' and pre_id = '%s' and type ='1' and tomb = '0';",\
                                        sockPath,UserName,id);
            //如果读取回来的数据不是空，就可以继续
            int ret = read_mysql(&mysql_info,connect);
            if(ret == -1)
            {
                //buf 返回
                strcpy(buf,"fail");
                printf("m cd 72:%s\n",buf);
                goto end;
                
            }
            else
            {
                bzero(&mysql_info, sizeof(mysql_info_t));
                sprintf(mysql_info.sql_commd,"select id from file_info where filename = '%s' and user = '%s' and pre_id = '%s' and tomb = '0';",sockPath,UserName,id);            
                ret = read_mysql(&mysql_info,connect);
                enStack(taskStack,mysql_info.sql_result);
            }
        }
        if(strcmp(sockPath,"..")==0)
        {
            deStack(taskStack);
        }
        sockPath = strtok(NULL,"/");
    }
    //传出一个当前路径
    end:
    pwd_mysql(taskStack, UserName, connect, buf);
    //printf("111%s111,%d\n",buf,strlen(buf));
    return 0;
}

//pwd
int pwd_mysql(dirStack_t *taskStack,char*UserName,MYSQL *connect,char*buf)
{
    mysql_info_t mysql_info;
    bzero(&mysql_info, sizeof(mysql_info_t));
    sprintf(mysql_info.sql_commd,"select filepath from file_info where user = '%s' and id = '%s'and tomb = '0';",UserName,taskStack->pFront->data);
    /// @brief pwd test
    int ret = read_mysql(&mysql_info,connect);
    strcpy(buf,mysql_info.sql_result);
    printf("pwd = %s\n",buf);
    return 0;
}

//ls
int ls_mysql(dirStack_t *taskStack,MYSQL *connect,char*buf)
{
    mysql_info_t mysql_info;
    bzero(&mysql_info, sizeof(mysql_info_t));
    sprintf(mysql_info.sql_commd,"select filename from file_info where pre_id = '%s' and tomb = 0;",taskStack->pFront->data);
    int ret = read_mysql(&mysql_info,connect);
    //传buf
    //拷贝一下buf
    strcpy(buf,mysql_info.sql_result);
    printf("buf = %s\n",buf);
    if(strlen(buf)!=0)
    {
        printf("ls = %s\n",buf);
    }
    else
    {
        strcpy(buf,"ls is empty!\n");
    }

    return 0;
}
